package cn.elwy.rcp.demo.dialog;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class TreeNodeItem implements Serializable {
  String database;
  Object item;
  Object data;
  List<TreeNodeItem> children = new LinkedList<TreeNodeItem>();
  boolean isDynamic = false;
  TreeNodeItem parent;
  Map datas = new HashMap();

  public Map getDatas() {
    return datas;
  }

  public void setDatas(Map datas) {
    this.datas = datas;
  }

  int start = 0;
  boolean checked = false;
  private String[] display = null;

  public String getDatabase() {
    return database;
  }

  public void setDatabase(String database) {
    this.database = database;
  }

  public Object getItem() {
    return item;
  }

  public void setItem(Object item) {
    this.item = item;
  }

  public List<TreeNodeItem> getChildren() {
    return children;
  }

  public void setChildren(List<TreeNodeItem> children) {
    this.children = children;
    try {
      TreeNodeItem item;
      for (Iterator<TreeNodeItem> it = children.iterator(); it.hasNext();) {
        item = it.next();
        item.setParent(this);
      }
    } catch (Exception e) {

    }
  }

  public void addChild(TreeNodeItem child) {
    if (children == null) {
      children = new LinkedList();
    }
    children.add(child);
    child.setParent(this);
  }

  public void removeChild(TreeNodeItem child) {
    children.remove(child);
  }

  public void addChild(int index, TreeNodeItem child) {
    children.add(index, child);
    child.setParent(this);
  }

  public boolean hasChildren() {
    return children == null ? false : children.size() > 0;
  }

  public boolean isDynamic() {
    return isDynamic;
  }

  public void setDynamic(boolean isDynamic) {
    this.isDynamic = isDynamic;
  }

  public TreeNodeItem getParent() {
    return parent;
  }

  public void setParent(TreeNodeItem parent) {
    this.parent = parent;
  }

  public Object getData() {
    return data;
  }

  public void setData(Object data) {
    this.data = data;
  }

  public void setData(String key, Object data) {
    datas.put(key, data);
  }

  public Object getData(String key) {
    return datas.get(key);
  }

  public int getStart() {
    return start;
  }

  public void setStart(int start) {
    this.start = start;
  }

  public boolean isChecked() {
    return checked;
  }

  public void setChecked(boolean checked) {
    this.checked = checked;
  }

  public String[] getTexts() {
    return display;
  }

  public void setTexts(String[] display) {
    this.display = display;
  }

  public String toString() {
    StringBuffer _string = new StringBuffer();
    if (item instanceof String[]) {
      if (parent != null) {
        _string.append(parent.toString());
      }
      String[] value = (String[]) item;
      for (int i = 0, j = value.length; i < j; i++) {
        if (_string.length() > 0) {
          _string.append(",");
        }
        _string.append(value[i]);
      }
    }
    // else if(item instanceof News_Document){
    // News_Document doc = (News_Document)item;
    // _string.append("NEWS_DOCUMENT:");
    // _string.append(doc.getId()).append(",").append(doc.getDocLib()+","+item.toString());
    // }else if(item instanceof News_Task){
    // News_Task task = (News_Task)item;
    // _string.append("NEWS_TASK:");
    // _string.append(task.getId()).append(",").append(task.getDocId());
    // }
    else {
      _string.append(item.toString());
    }

    return _string.toString();
  }
}
